﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com http://www.cszi.com
// </copyright>
//------------------------------------------------------------------------------
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Hosting;

namespace CS.Util.Http
{
    /// <summary>
    ///  Web环境下的服务器相关信息
    /// </summary>
    /// 
    /// <description class = "CS.Util.Http.Server">
    ///   
    /// </description>
    /// 
    /// <history>
    ///   2010-6-25 15:42:40 , zhouyu ,  创建	     
    ///  </history>
    public class Server
    {
        /// <summary>
        /// 站点虚拟应用路径
        /// </summary>
        public static string VirtualPath
        {
            get { return HostingEnvironment.ApplicationVirtualPath; }
        }

        /// <summary>
        /// 站点名称
        /// </summary>
        public static string SiteName
        {
            get { return HostingEnvironment.SiteName; }
        }

        static string _domain;
        /// <summary>
        /// 站点域名
        /// </summary>
        public static string Domain
        {
            get
            {
                if (_domain == null)
                {
                    if (HttpContext.Current != null)
                    {
                        var url = HttpContext.Current.Request.Url;
                        _domain = String.Format("{0}://{1}{2}", url.Scheme, url.Host, url.Port == 80 ? string.Empty : ":" + url.Port);
                    }
                    else
                        _domain = String.Empty;
                }
                return _domain;
            }
        }

        /// <summary>
        /// WebAppUrl路径
        /// </summary>
        public static string AppUrl
        {
            get
            {
                var appPath = VirtualPath;
                if (string.IsNullOrEmpty(appPath) || appPath.Length < 2)
                {
                    appPath = string.Empty;
                }
                return string.Format("{0}{1}/", Domain, appPath); ;
            }
        }


        /// <summary>
        /// Path 拼接
        /// </summary>
        /// <param name="paths">需要拼接的 Path 参数</param>
        /// <returns>拼接后的结果</returns>
        public static string CombinePath(params string[] paths)
        {
            var path = String.Join(Path.DirectorySeparatorChar + "", paths);
            path = path.Replace('/', Path.DirectorySeparatorChar);
            if (path.StartsWith("~" + Path.DirectorySeparatorChar) || path.StartsWith(Path.DirectorySeparatorChar + ""))
                path = Environment.Host.AppBaseDirectory + path.TrimStart(new[] { '~', Path.DirectorySeparatorChar });
            return path;
        }



    }
}
